package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.InvestDao;
import com.jk.dao.LenderDao;
import com.jk.dao.MediaDao;
import com.jk.entity.InvestEntity;
import com.jk.entity.MediaEntity;
import com.jk.utils.ReturnMsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 纪思齐
 * @version 1.0
 * @BelongsProject: 2203java-financial
 * @BelongsPackage: com.jk.service
 * @description: TODO  投资业务层
 * @date 2022/11/2 10:49
 */
@Service
public class InvestService {

    @Autowired
    private InvestDao investDao;

    @Autowired
    private MediaDao mediaDao;
    @Autowired
    private LenderDao lenderDao;


    public Map queryInvestmentList(InvestEntity investEntity) {
        try {
            List<InvestEntity> list = investDao.queryInvestmentList(investEntity);
//            QueryWrapper<InvestEntity> Wrapper = new QueryWrapper<>();
//            List<InvestEntity> list = investDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"查询成功,总共查询到"+list.size()+"条",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    public InvestEntity queryInvestmentGetById(Integer propertyId) {
        return investDao.queryInvestmentGetById(propertyId);
    }

    public InvestEntity queryInvestmentById(Integer propertyId) {
        InvestEntity investEntity = investDao.selectById(propertyId);
        return investEntity;
    }

    public Map IncreaseInvest(InvestEntity investEntity) {
        try {
            Integer  aa = Math.toIntExact(Math.round((Math.random() + 1) * 1000));
            //将系统时间转为字符串.
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(new Date());
            //剩余金额就是总金额.
            investEntity.setResidueMoney(investEntity.getPropertyResidue());
            //项目启动时间未系统时间.
            investEntity.setPropertyStarttime(format);
            investEntity.setPropertyId(aa);
            //项目进度未0.
            investEntity.setProjectSchedule("0");

            int invest = investDao.insert(investEntity);
            investEntity.setLender_id(aa);
            lenderDao.insert(investEntity);
            return ReturnMsgUtils.returnMsg(200,"新增成功",invest);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    public Map getMediaList() {
        try {
            QueryWrapper<MediaEntity> Wrapper = new QueryWrapper<>();
            List<MediaEntity> list = mediaDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"查询成功,总共查询到"+list.size()+"条",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    public Map addMedia(MediaEntity mediaEntity) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(new Date());
            //系统时间
            mediaEntity.setMediaDate(format);
            //访问量
            mediaEntity.setMediaVisitCount(1688);
            //点赞量
            mediaEntity.setMediaGoodCount(16);
            int insert = mediaDao.insert(mediaEntity);
            return ReturnMsgUtils.returnMsg(200,"新增成功",insert);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"新增失败",null);
        }
    }
}
